package com.bbx.taxi.client.widget.Dailog;

import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Activity.Wallet.CouponActivity;
import com.bbx.taxi.client.Util.IsActivityFinishUtils;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

/**
 * 新用户优惠劵
 */
public class MyPhotoDailog extends Dialog {
	private ImageView imageview;
	private boolean cancelable = true;
    private Context context;
    private boolean isFinish=false;
	public MyPhotoDailog(Context context) {
		super(context,R.style.Dialog_bocop);
		this.context=context;
		init();
	}

	private void init() {
		this.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
		View contentView = View.inflate(getContext(), R.layout.pop_first,
				null);
		setContentView(contentView);
		
		imageview=(ImageView)findViewById(R.id.pic_hb);
		imageview.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(context, CouponActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
				dismiss();
			}
		});
		this.setCancelable(true);
		contentView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (cancelable) {
					dismiss();
				}
			}
		});
	}

	@Override
	public void show() {
		if(!IsActivityFinishUtils.isActivityFinish(context)){
		super.show();
		Animation a = AnimationUtils.loadAnimation(context, R.anim.dialog_in);
		imageview.startAnimation(a);
		imageview.setVisibility(View.VISIBLE);
		isFinish=false;
		}
	}

	@SuppressLint("NewApi")
	@Override
	public void dismiss() {
		if(!IsActivityFinishUtils.isActivityFinish(context)){
		if(isFinish)
		super.dismiss();
		Animation a = AnimationUtils.loadAnimation(context, R.anim.dialog_out);
		imageview.startAnimation(a);
		a.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation anim) {}
			
			@Override
			public void onAnimationRepeat(Animation anim) {}	
			@Override
			public void onAnimationEnd(Animation anim) {
				isFinish=true;
				dismiss();
			}
		});
		}

	}
	@Override
	public void setCancelable(boolean flag) {
		cancelable = flag;
		super.setCancelable(flag);
	}
	public void setImage(Drawable drawable){
		imageview.setScaleType(ScaleType.FIT_CENTER);
		imageview.setImageDrawable(drawable);
	}
}
